package com.chentianyu.learn.javalinkmysql;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet(name = "linkMySQL",value = "/link-mysql")
public class LinkMySQLServlet extends HttpServlet {
    String url = "jdbc:mysql://localhost:3306/java_accout";
    String username = "root";
    String password = "1234567890";
    String message = "你好";
    public void init(){
        // 尝试连接到数据库
        try {
            Connection connection = DriverManager.getConnection(url, username, password);
            // 在此处执行 SQL 查询
            String sql = "select * from users";
            PreparedStatement stmt = connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();

            // 处理结果集
            while (rs.next()) {
                // 从结果集中获取数据
                String username = rs.getString("username");
                System.out.println(username);
            }
            // 关闭连接
            rs.close();
            stmt.close();
            connection.close();
        } catch (SQLException e) {
            // 处理连接错误
            e.printStackTrace();
        }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        String name = request.getParameter("name");

        PrintWriter out = response.getWriter();

        try {
            Connection connection = DriverManager.getConnection(url, username, password);
            // 在此处执行 SQL 查询
            String sql = "select * from users where username=?";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1,name);
            ResultSet rs = stmt.executeQuery();

            // 处理结果集
            while (rs.next()) {
                // 从结果集中获取数据
                String username = rs.getString("username");
                out.println("用户名:"+username);
            }
            // 关闭连接
            rs.close();
            stmt.close();
            connection.close();
        } catch (SQLException e) {
            // 处理连接错误
            e.printStackTrace();
        }

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        String returnMessageName = request.getParameter("username");
        String returnMessagePassWord = request.getParameter("password");

        PrintWriter out = response.getWriter();
        //out.println("返回值类型:"+returnMessage);

        try {
            Connection connection = DriverManager.getConnection(url, username, password);
            // 在此处执行 SQL 查询
            String sql = "insert into users(username,password) values(?,?);";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1,returnMessageName);
            stmt.setString(2,returnMessagePassWord);
            int result = stmt.executeUpdate();
            String resultStr;
            if(result == 1){
                resultStr = "注册成功";
            }else{
                resultStr = "注册失败";
            }

            // 处理结果集
            out.println("处理结果:" + resultStr);
            // 关闭连接
            //rs.close();
            stmt.close();
            connection.close();
        } catch (SQLException e) {
            // 处理连接错误
            e.printStackTrace();
        }
    }
}
